package com.huiyujiaoyu.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;


import com.huiyujiaoyu.http.httputil.SubscriberListener;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.util.AppManager;
import com.huiyujiaoyu.util.DisplayUtil;
import com.huiyujiaoyu.util.StatusBarCompat;
import com.huiyujiaoyu.util.ViewFindUtils;

import butterknife.ButterKnife;
import io.reactivex.internal.disposables.ListCompositeDisposable;


/**
 * Created by yujunlong on 2017/4/18.
 */

public abstract class BaseActivity extends AppCompatActivity implements SubscriberListener {
    private ListCompositeDisposable listCompositeDisposable ;
    protected String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    protected ProgressDialog pd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        beforeSetContentView(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        mContext = this;
        afterSetContentView(savedInstanceState);
        StatusBarCompat.compat(this);
        initView();
        initData();
        initListener();

    }
    protected void initView() {
    }

    protected void initData() {
    }

    protected void initListener() {
    }

    protected void beforeSetContentView(Bundle savedInstanceState) {
    }

    protected void afterSetContentView(Bundle savedInstanceState) {
    }
    @Override
    public void onNext(Object o, int httpcode) {

    }
    @Override
    public void onError(int httpcode) {

    }

    protected abstract int getLayoutId();
    public void setToolbar(Toolbar mToolbar) {
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            //隐藏标题栏
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
    }
    @Override
    protected void onDestroy() {
        clear();
        AppManager.getAppManager().removeActivity(this);
        mContext = null;
        super.onDestroy();
    }

    public ListCompositeDisposable getComp() {
        if (this.listCompositeDisposable == null) {
            this.listCompositeDisposable = new ListCompositeDisposable();
        }
        return listCompositeDisposable;
    }
    protected void clear() {
        if (!getComp().isDisposed()) {
            getComp().clear();
        }
    }
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }



    /**
     * 设置不带Title的标题，注意父布局是LinearLayout
     * @param
     */
    public void setTitleNoBar(){
        if ( Build.VERSION.SDK_INT >= 14) {
            View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
            if(view instanceof LinearLayout) {
                View header = LayoutInflater.from(mContext).inflate(R.layout.statusheight_layout,null);
                ViewGroup.LayoutParams params = ViewFindUtils.find(header,R.id.view_title).getLayoutParams();
                params.height = DisplayUtil.getStatusHeight(mContext);
                header.setLayoutParams(params);
                ((LinearLayout) view).addView(header, 0);
            }
        }
    }

    /**
     * 全局等待对话框
     */
    public void showWaitDialog() {
        //如果ctx等于空或者isFinishing
        if (mContext == null || isFinishing())
            return;
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pd == null
                        || !pd.isShowing()) {
                    pd = new ProgressDialog(mContext);
                    pd.setMessage("加载中...");
                    pd.setCancelable(true);
                    pd.show();
                }

            }
        });

    }

    public void dismissWaitDialog() {
        if (mContext == null || isFinishing())
            return;
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pd != null
                        && pd.isShowing()) {
                    pd.dismiss();
                    pd = null;
                }
            }
        });

    }

}
